package org.boyan.esb;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.boyan.cases.ExcelToCaseBody;
import org.boyan.cases.ExcelToDemandInfo;
import org.boyan.cases.ExcelToNonFunctionalDemand;
import org.boyan.entity.cases.DemandInfo;
import org.boyan.entity.cases.NonFunctionalDemand;
import org.boyan.entity.query.QueryInterface;
import org.boyan.entity.trans.Trans;
import org.boyan.utils.ExcelExportUtils;
import org.boyan.utils.ExcelUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class GenerateEsbExcel {

    public static void generateEsbExcel(File sourceFile,String targetPath)throws Exception{
        //解析sourceFile中的信息
        DemandInfo demandInfo= ExcelToDemandInfo.getDemandInfo(sourceFile);
        NonFunctionalDemand nonFunctionalDemand=ExcelToNonFunctionalDemand.getNonFunctionalDemand(sourceFile);
        //根据caseId找出List<Trans>
        List<Map<String,Object>> caseBodyList= ExcelToCaseBody.getCaseBody(sourceFile);
        File targetFile=new File(targetPath);
        String fileName = targetFile.getName();
        Workbook workbook = null;
        InputStream in = null;
        in = new FileInputStream(targetFile);
        if (ExcelUtils.judegExcelEdition(fileName)) {
            workbook = new XSSFWorkbook(in);
        } else {
            workbook = new HSSFWorkbook(in);
        }
        demandInfoToEsbExcel(caseBodyList,demandInfo,nonFunctionalDemand,workbook);
        caseBodyListToEsbExcel(caseBodyList,workbook);
        ExcelExportUtils.writeWorkbook(workbook,targetFile);
    }

    //修改esb接口文档第二个sheet
    private static void caseBodyListToEsbExcel(List<Map<String,Object>> caseBodyList, Workbook workbook)throws Exception{
        //先复制Sheet
        if(caseBodyList.size()>1){
            ExcelExportUtils.copySheet(workbook,1,caseBodyList.size()-1);
        }
        for(int i=1;i<workbook.getNumberOfSheets();i++){
            Trans trans= (Trans) caseBodyList.get(i-1).get("trans");
            workbook.setSheetName(i,trans.getTransCode());
            Sheet sheet=workbook.getSheetAt(i);
            sheet.getRow(0).getCell(1).setCellValue(trans.getTransCode());
            sheet.getRow(1).getCell(1).setCellValue(trans.getTransName());
            //获取输入，输出，表对象起始行
            int inputRow=getInputRow(sheet);
            if(inputRow==-1){
                throw new Exception("模板Excel文件可能出错了，解析失败");
            }
            List<QueryInterface> inputList=trans.getInput();
            //复制行
            ExcelExportUtils.insertRow(sheet,inputRow+1,6,inputList.size());
            for(int x=0;x<inputList.size();x++){
                sheet.getRow(inputRow+x+1).getCell(0).setCellValue(inputList.get(x).getEnName());
                sheet.getRow(inputRow+x+1).getCell(1).setCellValue(inputList.get(x).getCnName());
                sheet.getRow(inputRow+x+1).getCell(2).setCellValue(inputList.get(x).getDataType());
                sheet.getRow(inputRow+x+1).getCell(3).setCellValue(inputList.get(x).isInputFlag()==true?"Y":"N");
                sheet.getRow(inputRow+x+1).getCell(4).setCellValue(inputList.get(x).getQueryLogic());
            }
            int outputRow=getOutputRow(sheet);
            if(outputRow==-1){
                throw new Exception("模板Excel文件可能出错了，解析失败");
            }
            List<QueryInterface> outputList=trans.getOutput();
            ExcelExportUtils.insertRow(sheet,outputRow+1,6,outputList.size());
            for(int x=0;x<outputList.size();x++){
                sheet.getRow(outputRow+x+1).getCell(0).setCellValue(outputList.get(x).getEnName());
                sheet.getRow(outputRow+x+1).getCell(1).setCellValue(outputList.get(x).getCnName());
                sheet.getRow(outputRow+x+1).getCell(2).setCellValue(outputList.get(x).getDataType());
                sheet.getRow(outputRow+x+1).getCell(3).setCellValue(outputList.get(x).isInputFlag()==true?"Y":"N");
                sheet.getRow(outputRow+x+1).getCell(4).setCellValue(outputList.get(x).getQueryLogic());
            }
            int arrayStartRow=getArrayStartRow(sheet);
            if(arrayStartRow==-1){
                throw new Exception("模板Excel文件可能出错了，解析失败");
            }
            List<QueryInterface> arrayObjectList=trans.getArrayObject();
            ExcelExportUtils.insertRow(sheet,arrayStartRow+1,6,arrayObjectList.size());
            for(int x=0;x<arrayObjectList.size();x++){
                sheet.getRow(arrayStartRow+x+1).getCell(0).setCellValue(arrayObjectList.get(x).getEnName());
                sheet.getRow(arrayStartRow+x+1).getCell(1).setCellValue(arrayObjectList.get(x).getCnName());
                sheet.getRow(arrayStartRow+x+1).getCell(2).setCellValue(arrayObjectList.get(x).getDataType());
                sheet.getRow(arrayStartRow+x+1).getCell(3).setCellValue(arrayObjectList.get(x).isInputFlag()==true?"Y":"N");
                sheet.getRow(arrayStartRow+x+1).getCell(4).setCellValue(arrayObjectList.get(x).getQueryLogic());
            }
        }
    }

    //修改ESB文档第一个sheet
    private static void demandInfoToEsbExcel(List<Map<String,Object>> caseBodyList,DemandInfo demandInfo,NonFunctionalDemand nonFunctionalDemand,Workbook workbook){
        Sheet sheet=workbook.getSheetAt(0);
        //先根据交易数量复制行
        if(caseBodyList.size()>1){
            ExcelExportUtils.insertRow(sheet,1,25,caseBodyList.size()-1);
        }
        for(int i=0;i<caseBodyList.size();i++){
            Trans trans= (Trans) caseBodyList.get(i).get("trans");
            sheet.getRow(i+1).getCell(0).setCellValue(trans.getTransCode());
            sheet.getRow(i+1).getCell(1).setCellValue(trans.getTransName());
            sheet.getRow(i+1).getCell(5).setCellValue(demandInfo.getInvokeSystemAbbr());
            sheet.getRow(i+1).getCell(6).setCellValue(demandInfo.getDataSourceSystemAbbr());
            sheet.getRow(i+1).getCell(11).setCellValue(demandInfo.getInvokeSystem());
            sheet.getRow(i+1).getCell(14).setCellValue(demandInfo.getDataSourceSystem());
            sheet.getRow(i+1).getCell(16).setCellValue(demandInfo.getDemandContact());
            sheet.getRow(i+1).getCell(20).setCellValue(demandInfo.getInvokeSystemPrincipal());
            sheet.getRow(i+1).getCell(21).setCellValue(demandInfo.getDataSourceSystemPrincipal());
            sheet.getRow(i+1).getCell(22).setCellValue(demandInfo.getExpectedOperationDate());
            sheet.getRow(i+1).getCell(23).setCellValue("秒级".equals(nonFunctionalDemand.getResponseTime())?"1000":"60000");
            sheet.getRow(i+1).getCell(24).setCellValue("秒级".equals(nonFunctionalDemand.getResponseTime())?"1000":"60000");
            sheet.getRow(i+1).getCell(25).setCellValue(nonFunctionalDemand.getDailyTransAmount());
        }
    }

    //获取输入项的起始row
    private static int getInputRow(Sheet sheet){
        for(Row row:sheet){
            if("输入".equals(row.getCell(0).getStringCellValue())){
                return row.getRowNum();
            }
        }
        return -1;
    }

    //获取输出项的起始row
    private static int getOutputRow(Sheet sheet){
        for(Row row:sheet){
            if("输出".equals(row.getCell(0).getStringCellValue())){
                return row.getRowNum();
            }
        }
        return -1;
    }

    //获取表对象的起始row
    private static int getArrayStartRow(Sheet sheet){
        for(Row row:sheet){
            if("start".equalsIgnoreCase(row.getCell(4).getStringCellValue())){
                return row.getRowNum();
            }
        }
        return -1;
    }

    //获取表对象的结束row
    private static int getArrayEndRow(Sheet sheet){
        for(Row row:sheet){
            if("end".equalsIgnoreCase(row.getCell(4).getStringCellValue())){
                return row.getRowNum();
            }
        }
        return -1;
    }
}
